package core.proto.search;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import core.proto.Util;

import net.htmlparser.jericho.Segment;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import net.htmlparser.jericho.TextExtractor;

/**
 * This class takes care of extracting the html text that we want to use.
 */
public class HtmlTextExtractor extends TextExtractor {
	static final String IGNORE_ATTRIBUTE = "ignore";
	
	public HtmlTextExtractor(Segment segment) {
		super(segment);
	}

	public static TextExtractor createExtractor(File htmlFile) throws MalformedURLException, IOException{
		TextExtractor extractor = new HtmlTextExtractor(new Source(htmlFile.toURI().toURL()));
		extractor.setIncludeAttributes(false);
		extractor.setExcludeNonHTMLElements(true);
		return extractor;
	}

	static String extract(File htmlFile) throws MalformedURLException, IOException{
		return createExtractor(htmlFile).toString();
	}
	
	@Override
	/**
	 * 
	 */
	public boolean excludeElement(StartTag startTag){
		String ignore = startTag.getAttributeValue(IGNORE_ATTRIBUTE);
		if (ignore != null && ignore.equals("true")){
			return true;
		}
		return false;
	}
}
